package com.reins.bookstore.serviceimpl;

import com.reins.bookstore.dao.BookDao;
import com.reins.bookstore.dao.BasketDao;
import com.reins.bookstore.dao.UserDao;
import com.reins.bookstore.entity.Book;
import com.reins.bookstore.entity.Basketline;
import com.reins.bookstore.object.BasketlineDTO;
import com.reins.bookstore.object.OneBookOrder;
import com.reins.bookstore.service.BasketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;


@Service
public class BasketServiceImpl implements BasketService {

    @Autowired
    private BasketDao basketDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private BookDao bookDao;

    @Override
    public Basketline findBuylistById(Integer id){
        return basketDao.findById(id);
    }

    @Override
    public List<Basketline> findAllBuylists() {
        return basketDao.findAll();
    }

    @Override
    public List<BasketlineDTO> findAllBasketlinesByUserId(int id){
        List<Basketline> allList = new LinkedList<>();
        allList = basketDao.findAllBasketlinesByUserId(id);
        List<BasketlineDTO> res = new LinkedList<>();
        int len = allList.size();
        for(Basketline iter:allList)
        {
            Book add_book = bookDao.findById(iter.getBookId());
            // 只是给了书本信息和数量信息，总价是计算出来的，确保实时性
            BasketlineDTO add_dto = new BasketlineDTO(add_book,iter.getBookNum());

            res.add(add_dto);
        }
        return res;
    }

    @Override
    public Basketline saveBuylist(Basketline add){
        return basketDao.save(add);
    }

    @Override
    public Basketline editBuylist(Basketline edit){return basketDao.save(edit);}

    @Override
    public boolean deleteBuylistById(int id){
        boolean result = true;
        try{
            basketDao.deleteById(id);
        }
        catch (Exception ex){
            result = false;
        }
        return result;
    }

    @Override
    public BasketlineDTO saveOneBasketline(OneBookOrder params){
        Integer res_bookId = params.getBookId();
        Integer res_bookNum = params.getBookNum();
        Integer res_userId = params.getUserId();
        // 处理不存在的bookId和userId
        if(bookDao.findById(res_bookId) == null || userDao.findUserAuthById(res_userId) == null)
            return null;
        // 处理小于等于0的bookNum
        if(res_bookNum <= 0)
            return null;
        // 在购物车数据库中添加条目
        Basketline add_basketline = new Basketline(null,res_userId,res_bookId,res_bookNum);
        Basketline check_basketline = basketDao.checkBasketline(res_userId,res_bookId);
        if (check_basketline != null)   // 如果这个条目已经存在，那么先将原有条目删除，达到覆盖的效果
            basketDao.deleteById(check_basketline.getBasketlineId());
        basketDao.save(add_basketline);
        // 合成用于返回的结果
        Book mid_book = bookDao.findById(res_bookId);
        BasketlineDTO res = new BasketlineDTO(mid_book,res_bookNum);
        return res;
    }

    @Override
    public Integer deleteOneBasketline(OneBookOrder params){
        Integer res_bookId = params.getBookId();
        Integer res_userId = params.getUserId();
        // 只获取bookId和userId
        Basketline check_basketline = basketDao.checkBasketline(res_userId,res_bookId);
        if (check_basketline != null)
            basketDao.deleteById(check_basketline.getBasketlineId());
        else    // 没找到
            return 2;
        return 1;
        // 在购物车数据库中添加条目
    }

    @Override
    public Integer deleteAllByUserId(Integer userId){
        return basketDao.deleteAllByUserId(userId);
    }
}
